package com.haoshui.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haoshui.constants.SystemConstants;
import com.haoshui.domain.entity.Role;
import com.haoshui.domain.entity.UserRole;
import com.haoshui.mapper.RoleMapper;
import com.haoshui.service.RoleService;
import com.haoshui.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 角色信息表(SysRole)表服务实现类
 *
 * @author makejava
 * @since 2022-09-30 20:16:52
 */
@Service("sysRoleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

   @Autowired
   private UserRoleService userRoleService;

    @Override
    public List<String> selectRoleKeyByUserId(Long userId) {

        //用户id为1，为超级管理员，直接返回admin
        if(userId == 1L){
            List<String> result = new ArrayList<>();
            result.add("admin");
            return result;
        }

        LambdaQueryWrapper<UserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserRole::getUserId, userId);

        //获取用户的roleIds
        List<UserRole> userRoles = userRoleService.list(queryWrapper);
        List<Long> collect = userRoles.stream()
                .map(userRole -> userRole.getRoleId())
                .collect(Collectors.toList());

        //根据roleIds获取roles
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Role::getId, collect);
        List<Role> roles = list(wrapper);

        //取出RoleKey
        List<String> result = roles.stream()
                .map(Role::getRoleKey)
                .collect(Collectors.toList());

        return result;
    }

    @Override
    public Page<Role> findRole(Integer pageNum, Integer pageSize, String roleName, String status) {
        //添加判断条件
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Objects.nonNull(roleName) && StringUtils.hasText(roleName), Role::getRoleName, roleName);
        queryWrapper.like(Objects.nonNull(status) && StringUtils.hasText(status), Role::getStatus, status);
        queryWrapper.orderByAsc(Role::getRoleSort);
        //TODO 有可能逻辑删除会被查出来

        //制作页信息
        Page<Role> page = new Page<>(pageNum, pageSize);

        //页查询
        page(page, queryWrapper);

        return page;
    }

    @Override
    public boolean changeStatusById(Long roleId, String status) {
        //判断条件
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getId, roleId);

        //更新实体
        Role role = new Role();
        role.setStatus(status);
        boolean update = update(role, queryWrapper);


        return update;
    }

    @Override
    public Long addRole(Role role) {
        int insert = getBaseMapper().insert(role);

        return Long.valueOf(insert);
    }

    @Override
    public List<Role> listAllRole() {
        //状态正常的条件
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getStatus, SystemConstants.USER_STATUS_NORMAL);
        queryWrapper.orderByAsc(Role::getRoleSort);

        //查询
        List<Role> list = list(queryWrapper);

        return list;
    }

    @Override
    public List<Role> getAllRoles() {
        LambdaQueryWrapper<Role> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getStatus, SystemConstants.ROLE_NORMAL);
        List<Role> list = list(queryWrapper);
        return list;
    }


}
